<?php

require_once 'ActionFramework/start.php';
require_once 'views/all.cls.php';
require_once 'actions/try_reset_password.cls.php';

/**
 * Edit the criteria and return to the edit criteria view.
 */
class RequestPasswordResetAction extends Action {
	
	const INPUT_EMAIL = 'email';
	
	public function __construct() {
		parent::__construct();
		$this->add_input(new EmailInput(RequestPasswordResetAction::INPUT_EMAIL, "Email"));
	}
	
	public function trigger($request) {
		global $DB;
		$user = $DB->get_user_by_email($_REQUEST[RequestPasswordResetAction::INPUT_EMAIL]);
		if (!$user) {
			return r(new StartAction('The emailaddress is unknown'))->trigger($request);
		}
		
		$reset_link = r(new TryResetPasswordAction())->get_direct_link();
		$reset_link->set_value(TryResetPasswordAction::INPUT_SECRET, $DB->create_login_by_url_secret($user));
		$reset_link = current_page_url() . $reset_link->__toString();
		
		LT_mail($user->email, 'Password reset requested', "

Hi {$user->first_name},

Someone - we assume you - told us you want to reset your password. You can do so by following this link: $reset_link

The Language Exchange team

		");
		
		return r(new StartAction('An email has been sent. Click the link in the email to choose a new password.'))->trigger($request);
	}
}

?>